package servlet;

import constant.ExamConstant;
import service.PatientService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/registerPatient")
public class RegisterPatient extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置请求编码格式
        req.setCharacterEncoding("utf-8");
        // 设置响应编码格式
        resp.setContentType("text/html;charset=utf-8");
        //获取请求信息
        String patientId=req.getParameter("patientId");
        String patientName=req.getParameter("patientName");
        String patientAge=req.getParameter("patientAge");
        String patientPwd=req.getParameter("patientPwd");
        String patientSex=req.getParameter("patientSex");
        // 处理请求信息
        // 获取业务层对象
        PatientService patientService = new PatientService();
        boolean flag = patientService.regist(Integer.valueOf(patientId),patientName,patientPwd, Integer.valueOf(patientSex),Integer.valueOf(patientAge));
        // 响应处理结果
        if (!flag){// 用户名重复
            req.setAttribute("error","病人账号已存在，请重新填写");
            req.getRequestDispatcher("./registerPatient.jsp").forward(req,resp);
        } else {
            resp.sendRedirect(ExamConstant.HOME_PAGE_LOGIN);
        }
    }
}
